<?php
require_once("database.php");
$_SESSION[$sessionDepth] == "";
$bodyString = "";
if ($_SERVER['REQUEST_METHOD']=="POST") {
	$id = $_POST['id'];

	if ($id) {
		$sort_order = htmlentities($_POST["sort_order"],ENT_QUOTES);
		$title = htmlentities($_POST["title"],ENT_QUOTES);
		$image = htmlentities($_POST["image"],ENT_QUOTES);
		$image_thumb = htmlentities($_POST["image_thumb"],ENT_QUOTES);
		$section = htmlentities($_POST["section"],ENT_QUOTES);

		$SQLeditQuery = "UPDATE best_photos SET ";	
		$SQLeditQuery .= "  sort_order = '$sort_order',  title = '$title',  image = '$image',  image_thumb = '$image_thumb',  section = '$section'  "; 
		$SQLeditQuery .= " WHERE id = '$id' ";
		$SQLeditResult = $db->query($SQLeditQuery);
	}
	else {
		$SQLsortQuery = "SELECT MAX(sort_order) AS sort_order FROM best_photos";
		$SQLsortResult = $db->query($SQLsortQuery);
		$sortArray = $db->fetch_array($SQLsortResult);
		$sort_order = htmlentities($sortArray['sort_order']);
		$sort_order++;

		
		$title = htmlentities($_POST["title"],ENT_QUOTES);
		$image = htmlentities($_POST["image"],ENT_QUOTES);
		$image_thumb = htmlentities($_POST["image_thumb"],ENT_QUOTES);
		$section = htmlentities($_POST["section"],ENT_QUOTES);

$SQLnewQuery = "INSERT INTO best_photos (sort_order,title,image,image_thumb,section) VALUES ('$sort_order','$title','$image','$image_thumb','$section')";
		$SQLnewResult = $db->query($SQLnewQuery);
	} //end if(id) else
} // end if (request method = POST)

$SQLlistQuery = "SELECT * FROM best_photos ORDER BY sort_order ASC";
$SQLlistResult = $db->query($SQLlistQuery);
while ($rowArray = $db->fetch_array($SQLlistResult)) {
		$sort_order = $rowArray["sort_order"];
		$title = $rowArray["title"];
		$image = $rowArray["image"];
		$image_thumb = $rowArray["image_thumb"];
		$section = $rowArray["section"];
		$id = $rowArray["id"];

		$bodyString .= "\n<div class='backListRecord best_photos_record' id='$id'>";
		$bodyString .= "\n  <div class='backListElement'><div class='dragIcon'></div></div>";
		$bodyString .= "\n  <div class='backListElement backListTitle'>$title</div>";
		$imgFileTypes = array('JPG','PEG','GIF','PNG');
		$fileEXT = strtoupper( substr($image, -3) );
		if ( !in_array($fileEXT, $imgFileTypes) ) {
			$bodyString .= "\n  <img src='images/defaultThumbnail.png' class='backListElement'>";
		} else if ($image_thumb) {
			$bodyString .= "\n  <img src='$image_thumb' class='backListElement'>";
		}
						
		$bodyString .= "
  <div class='listPageButtons'>
    <a class='editBtn positiveBtns' href='best_photos_write.php?id=$id'>EDIT</a>
      <div class='deleteBtn'>
      <a class='negativeBtns'>DELETE</a>
      <div class='deleteConfirmation' id='deleteConfirmation$id'>
        Do you really want to delete? <a class='deleteYes'>YES</a> // <a class='deleteNo'>NO</a>
      </div>
    </div>
  </div>
</div>  <!--end .backListRecord id:$id-->
";
}
?>
<html>
	<head>
		<title>Photos List</title>
		<?php require_once('io_listPage_includes.php'); ?>
	<script type='text/javascript'>
	var jsTableName = 'best_photos';

	$(function() {
		jQuery.fileDelete = function(id,field) {
			$.ajax({
				url: "ajax.php",
				data: "tablename="+jsTableName+"&id="+id+"&field="+field+"&AJAXtask=deleteFile",
				cache: false,
				complete: function(){
					//alert("tablename="+jsTableName+"&id="+id+"&field="+field+"&AJAXtask=deleteFile");
				}
			});
		};	

		jQuery.deleteAllFiles = function(id,field) {
			$.fileDelete(ID,'image');
$.fileDelete(ID,'image_thumb');
		
		};		
	});
	</script>
	</head>
<body>
	<div class='navigation'>
	  <div id='goTo'>Menu...</div>
	  <div id="tableList">
    <?php 
      $currentNavTable = 'best_photos';
      require_once('navlist.php');
    ?>
	  </div> <!-- end #tableList -->
	  <div class='siteName'>TheBest.com</div>
	    <div id='currentTableTitle'><a href='best_photos_list.php'>Photos</a></div>
	    <div id='listBtns'>
	      <div id='listViewBtn'><img src='images/list-icon.gif' alt='List View' /></div>
	      <div id='gridViewBtn'><img src='images/grid-icon.gif' alt='Grid View' /></div>
	    </div>
	  </div><!-- End .navigation -->
	<div class='recordsList sortable'>
  <!-- <a class='newBtn positiveBtns' href='best_photos_write.php'>NEW RECORD</a> -->
	  <?php echo $bodyString; ?>
	  <a class='newBtn positiveBtns' href='best_photos_write.php'>NEW RECORD</a>
	</div>
	

	</body>
</html>